//index.js  用wxml生成
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')
let lib = Pager.lib
let mkWaterfall = require('components/modules/waterfall')
const util = require('../../utils/util.js')

const data1 = [
  {
    img: 'http://www.agzgz.com/imgs/appstorecover-1.jpg',
    width: '160px',
    height: '240px',
    title: '带你体验猪一样的幸福生活， 寻味佛山顺德两天一夜的...',
  },
  {
    img: 'http://www.agzgz.com/imgs/fruit.jpg',
    width: '160px',
    height: '160px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/glasy.jpg',
    width: '160px',
    height: '160px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/appstorecover-1.jpg',
    width: '160px',
    height: '240px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/fruit.jpg',
    width: '160px',
    height: '100px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/glasy.jpg',
    width: '160px',
    height: '120px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/appstorecover-1.jpg',
    width: '160px',
    height: '160px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/fruit.jpg',
    width: '160px',
    height: '200px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/glasy.jpg',
    width: '160px',
    height: '180px',
    title: '网易严选多功能私享头',
  },
  {
    img: 'http://www.agzgz.com/imgs/glasy.jpg',
    width: '160px',
    height: '200px',
    title: '网易严选多功能私享头',
  }
]

const adpater = (data, idx = 0) => {
  return data.map((item, ii) => {
    const _idx = parseInt(idx + ii)
    return {
      id: lib.suid('wf_'),
      // $$id: lib.suid('wf_'),
      img: {
        'lazy-load': true,
        itemClass: 'item-pic',
        itemStyle: '--img-height: '+item.height,
        mode: 'aspectFill',
        src: item.img,
        // src: '/images/cat.png',
        // bindload: 'bindload?src='+item.img
      },
      dot: [{
        title: _idx+'、'+item.title,
        itemClass: 'item-title'
      }],
      aim: 'onClick',
      catchlongpress: 'onDel',
      itemClass: 'abc_'+idx+'_'+ii
    }
  })
}

Pager({
  data: {
    waterfall: mkWaterfall({
      $$id: 'waterfallx',
      itemClass: 'demo-list-item',
      listClass: 'wrap-list demo-list',
      data: adpater(data1),
      header: {
        title: '真正的瀑布流，运用了grid的方式。注：图片不能用widthfix; 长按可删除',
        itemClass: 'ss-tag tag-fail m-default larger'
      }
    })
  },
  newData: [], 
  idx: 0,
  bindload(e, p, i) {
    console.log('======', e, p, i, this);
    
  },
  onDel(e, params, inst) {
    const _id = e.currentTarget.id
    this.$list.delete({id: _id}, function () {
      wx.showToast({title: "已删除该产品"})
    })
  },
  onReady() {
    this.$list = this.getElementsById('waterfallx')
  },
  onReachBottom() {
    const that = this
    if (this.idx < 3) {
      this.idx ++
      this.$list.insertx(adpater(data1, this.$list.getData().data.length))
    }
    else {
      if (!this.isAd) {
        util.stimulateAD('adunit-c070332b96523301', function() {
          that.isAd = true
          this.idx ++
          that.$list.insertx(adpater(data1, that.$list.getData().data.length))
        }, '如需要再显示下一页数据的效果，请观看完广告')
      }
      else {
        this.idx ++
        that.$list.insertx(adpater(data1, that.$list.getData().data.length))
      }
    }
    if (this.idx > 20) {
      this.$list.loaded()
    }
  },
  onLoad() {
  }
})